package cn.zm.app.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.zm.app.pojo.BackendUser;
import cn.zm.app.pojo.DevUser;
import cn.zm.app.service.DevUserService;

@Controller
@RequestMapping("dev")
public class DevUserController {

	@Resource
	private DevUserService devUserService;

	/*
	 * 跳转至选择登录页面
	 */
	@RequestMapping("/login")
	public String toLogin() {
		return "index";
	}

	/*
	 * 用户登录验证页面
	 */
	@RequestMapping("/doLogin")
	public String doLogin(
			@RequestParam(value = "devCode", required = false) String devCode,
			@RequestParam(value = "devPassword", required = false) String pass,
			Model model, HttpSession session) {
		if (null == devCode) {
			return "jsp/devlogin";
		} else {
			DevUser du = devUserService.selectUser2(devCode, pass);
			if (null != du) {
				if (pass.equals(du.getDevPassword())) {
					session.setAttribute("devUserSession", du);
					return "jsp/developer/main";
				} else {
					model.addAttribute("error", "密码错误！！");
					model.addAttribute("devCode",devCode );
					return "jsp/devlogin";
				}
			} else {
				model.addAttribute("error", "账号不存在！");
				model.addAttribute("devCode",devCode );
				return "jsp/devlogin";
			}
		}
	}
	
	@RequestMapping("/tolist")
	public String toList(){
		return "jsp/developer/appinfolist";
	}

	/*
	 * 管理员登录验证页面
	 */
	@RequestMapping("/doLoginHou")
	public String doLoginHou(
			@RequestParam(value = "userCode", required = false) String userCode,
			@RequestParam(value = "userPassword", required = false) String pass,
			Model model, HttpSession session) {
		if (null == userCode) {
			return "jsp/backendlogin";
		} else {
			BackendUser bu = (BackendUser)devUserService.selectBackend(userCode);
			if (null != bu) {
				if (pass.equals(bu.getUserPassword())) {

					session.setAttribute("userSession", bu);
					return "jsp/backend/main";
				} else {
					model.addAttribute("userCode",userCode);
					model.addAttribute("error", "密码错误！！");
					return "jsp/backendlogin";
				}
			} else {
				model.addAttribute("userCode",userCode);
				model.addAttribute("error", "账号不存在！");
				return "jsp/backendlogin";
			}
		}
	}
}
